﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DeploymentMaxLib
{
    public abstract class ActionFileSystemBase : ActionBase
    {
        public void DeleteDirectoryAndFiles(DirectoryInfo pDirectoryInfo)
        {
            DeleteDirectoryAndFiles(pDirectoryInfo, true, false);
        }

        public void DeleteDirectoryAndFiles(DirectoryInfo pDirectoryInfo, bool pAddErrorsToResult)
        {
            DeleteDirectoryAndFiles(pDirectoryInfo, true, pAddErrorsToResult);
        }

        public void DeleteDirectoryAndFiles(string pDirectory, bool pDeleteThisDir)
        {
            if (System.IO.Directory.Exists(pDirectory))
            {
                DeleteDirectoryAndFiles(new DirectoryInfo(pDirectory), pDeleteThisDir, false);
            }
        }

        public void DeleteDirectoryAndFiles(string pDirectory, bool pDeleteThisDir, bool pAddErrorsToResult)
        {
            if (System.IO.Directory.Exists(pDirectory))
            {
                DeleteDirectoryAndFiles(new DirectoryInfo(pDirectory), pDeleteThisDir, pAddErrorsToResult);
            }
        }

        public void DeleteDirectoryAndFiles(DirectoryInfo pDirectoryInfo, bool pDeleteThisDir, bool pAddErrorsToResult)
        {

            foreach (FileInfo tFileInfo in pDirectoryInfo.GetFiles())
            {

                tFileInfo.MakeWritable();


                try
                {
                    if (!tFileInfo.IsReadOnly)
                    {
                        tFileInfo.Delete();
                        base.AddLog(ActionLogLevelType.Medium, "DELETED File: {0}.", tFileInfo.Name);
                    }
                    else
                    {
                        base.AddLog(ActionLogLevelType.High, "ERROR File: {0} is Read Only.", tFileInfo.Name);

                        if (pAddErrorsToResult)
                        {
                            base.Result.AddResult(string.Format("ERROR File: {0} is Read Only.", tFileInfo.Name), ActionResultSeverityType.Error);
                        }
                    }

                }
                catch (Exception exc)
                {
                    if (pAddErrorsToResult)
                    {
                        base.Result.AddResult(exc);
                    }

                    base.AddLog(ActionLogLevelType.High, "ERROR File: {0} Exception: {1}.", tFileInfo.Name, exc.Message);
                }

            }

            foreach (DirectoryInfo tSubDirectoryInfo in pDirectoryInfo.GetDirectories())
            {
                DeleteDirectoryAndFiles(tSubDirectoryInfo);
            }

            if (pDeleteThisDir)
            {
                pDirectoryInfo.MakeWritable();

                try
                {
                    var tFileCountLeft = pDirectoryInfo.GetFiles().Length;
                    if (tFileCountLeft == 0)
                    {
                        pDirectoryInfo.Delete();
                        base.AddLog(ActionLogLevelType.Medium, "DELETED Directory: {0}.", pDirectoryInfo.Name);
                    }
                    else
                    {
                        if (pAddErrorsToResult)
                        {
                            base.Result.AddResult(string.Format("ERROR Directory: {0} has {1} files.", pDirectoryInfo.Name, tFileCountLeft), ActionResultSeverityType.Error);
                        }

                        base.AddLog(ActionLogLevelType.High, "ERROR Directory: {0} has {1} files.", pDirectoryInfo.Name, tFileCountLeft);
                    }

                }
                catch (Exception exc)
                {
                    base.AddLog(ActionLogLevelType.High, "ERROR Directory: {0} Exception: {1}.", pDirectoryInfo.Name, exc.Message);

                    if (pAddErrorsToResult)
                    {
                        base.Result.AddResult(exc);
                    }
                }
            }

        }
    }
}
